package cn.ibobo.lease.common.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;

/**
 * Description: 获取ip地址工具类 <br /> 
 * Project: meirenhui <br />
 * ClassName: IpUtil <br />
 * Copyright: Copyright (c) 2017 deshang<br />
 * @author 贾梦莹
 * @version 1.0 2017年11月20日下午2:59:36
 */
public class IpUtil {

	/**
	 * 获取ip地址
	 * @author 贾梦莹
	 * @createDate 2017年12月4日下午2:23:53
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    //根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { 
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ipAddress;
    }
	
	/**
	 * 获取ip所在地
	 * @author 贾梦莹
	 * @createDate 2017年12月4日下午2:43:31
	 * @return
	 */
	public static String getIpArea(HttpServletRequest request) {
		try {  
			URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + getIpAddr(request));    
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.connect(); 
			// 获取输入流  
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));  
			String line;  
			StringBuilder sb = new StringBuilder();  
			while ((line = br.readLine()) != null) {
				sb.append(line);  
			}  
			br.close();
			connection.disconnect();
			String str = new String(sb.toString());
			String data = "";
			Map<?, ?> mapType = JSON.parseObject(str, Map.class);  
	        for (Object obj : mapType.keySet()) {
	        	if(obj == "data") {
	        		data = mapType.get(obj).toString();
	        	}
	        }  
	        return data;
		} catch (Exception e) {  
			e.printStackTrace();  
		} 
		return null;
	}
	
}
